import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:pp_uikit/pp_uikit.dart';

class GroupDemoPage extends StatelessWidget {
  const GroupDemoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: GroupWidgetDemoPage(),
    );
  }
}

class GroupWidgetDemoPage extends StatefulWidget {
  GroupWidgetDemoPage({Key? key}) : super(key: key);

  @override
  _GroupWidgetDemoPageState createState() => _GroupWidgetDemoPageState();
}

class _GroupWidgetDemoPageState extends State<GroupWidgetDemoPage> {
  var flag = false.obs;

  /// 点击页面是否收起键盘
  bool isTapPageHideKeyboard() {
    return true;
  }

  void hideKeyboard(BuildContext context) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus!.unfocus();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("分组表单（PPGroup）"),
      ),
      body: GestureDetector(
        onTap: () {
          if (isTapPageHideKeyboard()) {
            hideKeyboard(context);
          }
        },
        onPanDown: (details) {
          if (isTapPageHideKeyboard()) {
            hideKeyboard(context);
          }
        },
        child: SingleChildScrollView(
          child: Column(
            children: [
              PPGroup(
                title: "PPGroupTitle例子，我是有title的",
                children: [
                  PPGroupTitle(
                    title: "联系人",
                    subTitle: "村名A",
                    isShowRightIcon: false,
                  ),
                  PPGroupTitle(
                    title: "买家留言",
                    subTitle:
                        "多行文本，顶部对齐，多行文本，顶部对齐，多行文本，顶部对齐，多行文本，顶部对齐，多行文本，顶部对齐，多行文本，顶部对齐，",
                    isShowRightIcon: false,
                    themeData: PPGroupTheme(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      groupPadding: const EdgeInsets.only(top: 10, bottom: 10),
                    ),
                  ),
                  PPGroupTitle(
                    title: "省市区",
                    subTitle: "广东-广州-海珠区",
                    isRequire: true,
                    onTap: () {
                      PPToast.show("点击触发回调_onTap");
                    },
                  ),
                ],
              ),
              PPGroup(
                children: [
                  PPGroupTitle(
                    title: "省市区",
                    subTitle: "广东-广州-海珠区",
                    onTap: () {
                      PPToast.show("点击触发回调_onTap");
                    },
                  ),
                ],
              ),
              PPGroup(
                title: "PPGroupSwitch例子",
                children: [
                  Obx(
                    () => PPGroupSwitch(
                      title: "7天无理由",
                      value: flag.value,
                      onChanged: (value) {
                        flag.value = value;
                      },
                    ),
                  ),
                ],
              ),
              PPGroup(
                title: "PPGroupInput例子",
                children: [
                  PPGroupInput(
                    controller: TextEditingController()..text = "中华广场3号",
                    // contentText: "中华广场3号",
                    isRequire: true,
                    title: "详细地址",
                    hint: "请输入",
                    onChanged: (newValue) {
                      PPToast.show("点击触发回调_${newValue}_onChanged");
                    },
                    onSubmitted: (text) {
                      PPToast.show("onSubmitted 回调_$text");
                    },
                  ),
                  PPGroupInput(
                    controller: TextEditingController(),
                    isRequire: true,
                    inputType: TextInputType.number,
                    inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                    title: "数字",
                    hint: "请输入",
                    onChanged: (newValue) {
                      PPToast.show("点击触发回调_${newValue}_onChanged");
                    },
                  ),
                  PPGroupInput(
                    contentText: "不可编辑",
                    isEdit: false,
                    title: "联系人",
                    hint: "请输入",
                    onChanged: (newValue) {
                      PPToast.show("点击触发回调_${newValue}_onChanged");
                    },
                  ),
                ],
              ),
              PPGroup(
                title: "PPGroupBlockInput例子",
                children: [
                  PPGroupBlockInput(
                    controller: TextEditingController()..text = "hello",
                    isRequire: true,
                    isEdit: true,
                    maxLength: 100,
                    title: "备注",
                    hint: "请输入",
                    onChanged: (newValue) {
                      PPToast.show("点击触发回调_${newValue}_onChanged");
                    },
                  ),
                  PPGroupBlockInput(
                    isRequire: true,
                    isEdit: false,
                    contentText: "hello world",
                    title: "备注",
                    subTitle: "下面内容不能编辑",
                    hint: "请输入",
                    onChanged: (newValue) {
                      PPToast.show("点击触发回调_${newValue}_onChanged");
                    },
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}
